/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.RecvsmsHist;
import com.thienloc.omap.entity.TrafficReport;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.slf4j.LoggerFactory;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
 *
 * @author GiangLT
 */
@Stateless
public class RecvsmsHistFacade extends AbstractFacade<RecvsmsHist> {

    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public RecvsmsHistFacade() {
        super(RecvsmsHist.class);
    }

    public List<RecvsmsHist> findByRecvTimeRange(Date startTime, Date endTime) {
        try {
            TypedQuery<RecvsmsHist> q = em.createNamedQuery("RecvsmsHist.findByRecvTimeRange", RecvsmsHist.class);
            q.setParameter("startTime", startTime);
            q.setParameter("endTime", endTime);
            List<RecvsmsHist> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(RecvsmsHistFacade.class.getName()).error("", ex);
            return null;
        }
    }

    public List<TrafficReport> getTrafficReport(Date startDate, Date endDate, String serviceName, String cpNumber) {
        try {
            String queryStr =
                    "select trunc(rh.recv_time) RECEIVED_TIME, rh.servicetype SERVICE, rh.srcaddr CP_NUMBER, count(trunc(rh.recv_time)) TOTAL_RECEIVED"
                    + " from recvsms_hist rh"
                    + " where rh.recv_time between :startDate and :endDate"
                    + " and rh.servicetype = :serviceName"
                    + " and rh.srcaddr = :cpNumber"
                    + " group by trunc(rh.recv_time), rh.servicetype, rh.srcaddr";
            Query query = em.createNativeQuery(queryStr);
            query.setParameter("startDate", startDate);
            query.setParameter("endDate", endDate);
            query.setParameter("serviceName", serviceName);
            query.setParameter("cpNumber", cpNumber);

            List result = query.getResultList();
            List<TrafficReport> trafficReports = new ArrayList<TrafficReport>(result.size());
            Iterator it = result.iterator();
            while (it.hasNext()) {
                Object[] fields = (Object[]) it.next();
                TrafficReport report = new TrafficReport();
                report.setDate((Date) fields[0]);
                report.setService((String) fields[1]);
                report.setCpNumber((String) fields[2]);
                report.setTotalMessage((Integer) fields[3]);

                trafficReports.add(report);
            }
            return trafficReports;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            LoggerFactory.getLogger(RecvsmsHistFacade.class.getName()).error("", ex);
            return null;
        }
    }
}
